<?php

namespace App\Console\Commands;

use App\Services\Designer\Model\DesignerDemand;
use App\Services\Designer\Model\DesignerDemandAlbum;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ERPSyncUidCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:sync:uid';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'ERP user sync uid.';



    public function __construct()
    {
        parent::__construct();
    }

    /**
     * TODO 把设计师俱乐部需求相关的表中的designer_id全部修改为对应erp用户的uid
     * @throws \Throwable
     */
    public function handle()
    {
        $designers = DB::table('kd_designers')->get();
        DB::transaction(function()use($designers){
            foreach ($designers as $designer) {
                if ($designer->erp_user_id && ($designer->erp_user_id > 0)) {
                    DesignerDemand::query()->where('designer_id', $designer->id)->update(['designer_id' => $designer->erp_user_id]);
                    DesignerDemandAlbum::query()->where('designer_id', $designer->id)->update(['designer_id' => $designer->erp_user_id]);
                    DB::table('kd_designer_demand_bidding')->where('designer_id', $designer->id)->update(['designer_id' => $designer->erp_user_id]);
                    DB::table('kd_designer_demand_programme')->where('designer_id', $designer->id)->update(['designer_id' => $designer->erp_user_id]);
                }
            }
        });
        return "";
    }
}
